/**
 * @Title: CustomerImpl
 * @Package cn.com.yiqi.wsc.service.customer.impl
 * @Description: TODO
 * Copyright: Copyright (c) 2016
 * Company: 成都壹柒互动科技有限公司
 * @author 会员系统组-林威
 * @date 2017/2/17 14:11
 * @version V1.0
 */
package cn.com.yiqi.wsc.service.customer.impl;

import cn.com.artemis.framework.service.message.utils.MobilePhoneUtil;
import cn.com.yiqi.wsc.CityService;
import cn.com.yiqi.wsc.Ro.AddressVO;
import cn.com.yiqi.wsc.Ro.CustomerRo;
import cn.com.yiqi.wsc.Ro.SpreadVO;
import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultUtil;
import cn.com.yiqi.wsc.dao.appparam.entity.AppParamEntity;
import cn.com.yiqi.wsc.dao.customer.*;
import cn.com.yiqi.wsc.dao.point.PointFetchMapper;
import cn.com.yiqi.wsc.dao.shop.ItemMapper;
import cn.com.yiqi.wsc.entity.customer.*;
import cn.com.yiqi.wsc.entity.order.OrderMain;

import cn.com.yiqi.wsc.entity.shop.Item;
import cn.com.yiqi.wsc.enumeration.PointBizType;
import cn.com.yiqi.wsc.ro.WeixinInfoRO;
import cn.com.yiqi.wsc.service.BaseService;
import cn.com.yiqi.wsc.service.SMS.SmsService;
import cn.com.yiqi.wsc.service.SMS.enumation.SmsCodeType;
import cn.com.yiqi.wsc.service.balance.BalanceService;
import cn.com.yiqi.wsc.service.balance.dataobject.BalanceRecordDO;
import cn.com.yiqi.wsc.service.balance.enumeration.BalanceType;
import cn.com.yiqi.wsc.service.customer.CustomerService;
import cn.com.yiqi.wsc.service.customer.common.CustomerSeqUtil;
import cn.com.yiqi.wsc.service.customer.common.CustomerStatusEnum;
import cn.com.yiqi.wsc.service.point.PointService;
import cn.com.yiqi.wsc.service.point.dataobject.PointOrderDO;
import cn.com.yiqi.wsc.service.sysparams.AppParamService;
import cn.com.yiqi.wsc.service.sysparams.SysParamsService;
import cn.com.yiqi.wsc.service.sysparams.dataobject.DistributIonDO;
import cn.com.yiqi.wsc.Ro.PointParaDO;
import cn.com.yiqi.wsc.util.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.*;
import java.util.Calendar;
import java.util.Date;

/**
 * @author 会员系统组-林威
 * @ClassName: CustomerImpl
 * Description: TODO
 * @date 2017/2/17 14:11
 */
@Service
public class CustomerImpl extends BaseService implements CustomerService {
    @Autowired
    CustomerMapper customerDao;

    @Autowired
    CityService cityService;

    @Autowired
    CustomerAuthorizationInfoMapper authDao;

    @Autowired
    CustomerAddressMapper addressDao;

    @Autowired
    ItemMapper itemDao;

    @Autowired
    SysParamsService sysParamsService;

    @Autowired
    PointService pointService;

    @Autowired
    @Qualifier("xxtSmsService")
    SmsService smsService;

    @Autowired
    CustomerMentionDao mentionDao;

    @Autowired
    SpreadSaleAllotDao spreadDao;

    @Autowired
    AppParamService paramService;

    @Autowired
    SpreadSaleDao spreadSaleDao;

    @Autowired
    BalanceService balanceService;

    @Autowired
    PointFetchMapper fetchDao;
    /**
     * 查找用户基本信息
     */
    public Customer selectById(Integer id) {
        if (!ResultUtil.isSuccess(isExist(id))) {
            return null;
        }
        return customerDao.selectByPrimaryKey(id);

    }

    @Transactional
    @Override
    public CustomerRo updateOrInsertUser(WeixinInfoRO ro, Integer pid) {
        CustomerRo customerVo = customerDao.findByOpenIdRegist(ro.getOpenid());
        if (customerVo == null) {
            //新增操作
            Customer customer = new Customer();
            if (null != pid && pid != 0) {
                customer.setPid(pid);
                //分销关系
                Customer puser = customerDao.selectByPrimaryKey(pid);
                if (null != puser) {
                    customer.setSeq(CustomerSeqUtil.setSeq(puser.getSeq(), pid));
                }
            } else {
                customer.setPid(0);
                customer.setSeq(".0.");
            }

            customer.setBalance(BigDecimal.ZERO);
            customer.setCode(nextCustomerUid() + "");
            customer.setGrouthValue(0);

            customer.setStatus(ro.getSubscribe() == 1 ? Byte.parseByte(CustomerStatusEnum.attention.getValue() + "")
                    : Byte.parseByte(CustomerStatusEnum.normal.getValue() + ""));
            customer.setEnable(Byte.parseByte("1"));
            customer.setPoint(0);
            customer.setDealAmount(BigDecimal.ZERO);
            customer.setSubcount(0);
            customer.setDealTimes(0);
            customer.setVip(Byte.parseByte("0"));
            customer.setInsertTime(new Date());
            customer.setAvatar(ro.getHeadimgurl());
            customer.setNick(ro.getNickname());
            customer.setSex(Byte.parseByte(ro.getSex() + ""));
            customer.setCountry(ro.getCountry());
            String proCode = cityService.findProvicnCode(ro.getProvince());
            if (null != proCode) {
                customer.setProvince(proCode);
                customer.setCity(cityService.findCityByProviceAndCityName(proCode, ro.getCity()));
            }
            customer.setType(1);
            customer.setLastLoginTime(new Date());
            customerDao.insertSelective(customer);
            //插入微信信息ms_customer_authorization_info
            CustomerAuthorizationInfo info = new CustomerAuthorizationInfo();
            info.setLoginType(Byte.parseByte("1"));
            info.setInsertTime(new Date());
            info.setLastLoginTime(new Date());
            info.setCustomerId(customer.getId());
            info.setLoginAccount(ro.getOpenid());
            info.setLoginCount(1);
            info.setLoginToken(ro.getToken());
            info.setLoginRefreshToken(ro.getRefreshToken());
            authDao.insertSelective(info);
        } else {
            //跟新微信资料
            Customer customer = new Customer();
            customer.setId(customerVo.getId());
            customer.setAvatar(ro.getHeadimgurl());
            customer.setNick(ro.getNickname());
            customer.setSex(Byte.parseByte(ro.getSex() + ""));
            customer.setCountry(ro.getCountry());
            customer.setLastLoginTime(new Date());
            String proCode = cityService.findProvicnCode(ro.getProvince());
            customer.setStatus(ro.getSubscribe() == 1 ? Byte.parseByte(CustomerStatusEnum.attention.getValue() + "")
                    : Byte.parseByte(CustomerStatusEnum.normal.getValue() + ""));
            if (null != proCode) {
                customer.setProvince(proCode);
                customer.setCity(cityService.findCityByProviceAndCityName(proCode, ro.getCity()));
            }
            customerDao.updateByPrimaryKeySelective(customer);
            CustomerAuthorizationInfo info = authDao.findByCusomerIdWeixin(customer.getId());
            info.setLastLoginTime(new Date());

            info.setLoginCount(info.getLoginCount() == null ? 1 : (info.getLoginCount() + 1));
            info.setLoginToken(ro.getToken());
            info.setLoginRefreshToken(ro.getRefreshToken());
            authDao.updateByPrimaryKeySelective(info);
        }
        return customerDao.findByOpenId(ro.getOpenid(),null);
    }

    /**
     * 判断用户是否存在并未被禁用
     *
     * @param id 用户id
     */
    @Override
    public Result<Customer> isExist(Integer id) {
        Result<Customer> re = new Result<Customer>();
        if (id == null) {
            return ResultUtil.fail(re, "用户不存在或已被禁用");
        }
        Customer user = customerDao.isExist(id);
        if (null != user) {
            re.setReturnValue(user);
            return ResultUtil.success(re);
        } else {
            return ResultUtil.fail(re, "用户不存在或已被禁用");
        }
    }

    /**
     * 收藏
     */
    @Transactional
    @Override
    public void like(Integer userId, Integer itemId) {
        //商品是否存在
        Item item = itemDao.selectByPrimaryKey(itemId);
        if (item == null) {
            return;
        }
        //是否已经收藏
        if (null != customerDao.haveLiked(userId, itemId)) {
            return;
        }
        //点赞
        customerDao.like(userId, itemId, new Date());
    }

    /**
     * 取消收藏
     */
    @Transactional
    @Override
    public Integer deletelike(Integer userId, Integer itemId) {
        return customerDao.cancelLike(userId, itemId);
    }


    @Override
    public boolean isDelete(String openid) {
        if(openid == null ){
            return true;
        }

        CustomerRo ro = customerDao.findByOpenIdRegist(openid);
        if (ro == null) {
            return false;
        } else {
            return ro.getStatus() == 2 && ro.getEnable() == 1;
        }
    }

    /**
     * 查询所有收货地址
     */
    public List<AddressVO> findAllAddress(Integer userId) {
        return addressDao.findAddress(userId);
    }

    /**
     * 设为默认地址
     */
    @Transactional
    public void setDefaultAddress(Integer id) {
        CustomerAddress address = addressDao.selectByPrimaryKey(id);
        if (address != null) {
            //将原来的默认设为非默认
            addressDao.cancelOldDefault(address.getCustomerId());
            //设置新的默认地址
            address.setIsDefault(Byte.parseByte("1"));
            addressDao.updateByPrimaryKeySelective(address);
        }

    }

    /**
     * 更新收货地址
     */
    @Transactional
    public void updateAddress(AddressVO vo) {
        CustomerAddress address = addressDao.selectByPrimaryKey(vo.getId());
        if (null != vo.getCityCode()) {
            address.setCity(vo.getCityCode());
        }
        if (null != vo.getLinkName()) {
            address.setLinkName(vo.getLinkName());
        }
        if (null != vo.getLinkTel()) {
            address.setLinkTel(vo.getLinkTel());
        }
        if(null != vo.getAddress()){
            address.setAddress(vo.getAddress());
        }
        if (null != vo.getIsDefault()) {
            address.setIsDefault(Byte.parseByte(vo.getIsDefault() ? "1" : "0"));
        }
        if (address.getIsDefault() == 1) {
            //将原来的默认设为非默认
            addressDao.cancelOldDefault(address.getCustomerId());
        }
        addressDao.updateByPrimaryKeySelective(address);
    }

    /**
     * 新增收货地址
     */
    @Transactional
    public void insertAddress(AddressVO vo) {
        CustomerAddress address = new CustomerAddress();
        if (null != vo.getIsDefault()) {
            address.setIsDefault(Byte.parseByte(vo.getIsDefault() ? "1" : "0"));
        } else {
            address.setIsDefault(Byte.parseByte("0"));
        }
        address.setType(Byte.parseByte("3"));
        address.setLinkTel(vo.getLinkTel());
        address.setLinkName(vo.getLinkName());
        address.setInsertTime(new Date());
        address.setCity(vo.getCityCode());
        address.setLinkTel(vo.getLinkTel());
        address.setAddress(vo.getAddress());
        address.setCustomerId(vo.getCustomerId());
        if (address.getIsDefault() == 1) {
            //将原来的默认设为非默认
            addressDao.cancelOldDefault(address.getCustomerId());
        }
        addressDao.insertSelective(address);
    }

    public AddressVO findaddressByid(Integer id) {
        return addressDao.findaddressByid(id);
    }

    /**
     * 判断用户和用户收货地址存在
     */
    public Result<Customer> isExistUserAndAddress(Integer userId, Integer addressId) {
        Result re = new Result();
        if (null == userId || null == addressId) {
            return ResultUtil.fail(re, "请填写收货地址");
        }
        re = isExist(userId);
        if (!ResultUtil.isSuccess(re)) {
            return re;
        }
        CustomerAddress address = addressDao.selectByPrimaryKey(addressId);
        if (address == null || !address.getCustomerId().equals(userId)) {
            return ResultUtil.fail(re, "请填写收货地址");
        }
        return ResultUtil.success(re);
    }

    @Override
    public void updateCustomer(Customer customer) {
        customerDao.updateByPrimaryKeySelective(customer);
    }

    @Override
    public Result canExchange(Integer userId, Integer exchangePoint) {
        Result re = new Result();
        Customer customer = customerDao.selectByPrimaryKey(userId);
        if (customer.getVip() != 1) {
            re.putData("openVip", true);
            return ResultUtil.fail(re, "抱歉，您不是我们的会员，不能享受积分兑换服务");
        }
        Integer point = customer.getPoint();
        if (point < exchangePoint) {
            return ResultUtil.fail(re, "积分不足");
        }
        PointParaDO pointPara = sysParamsService.getPointPara();
        if(exchangePoint%pointPara.getPoint2balanceScale() != 0){
            return ResultUtil.fail(re,"积分必须为"+pointPara.getPoint2balanceScale()+"的整数倍");
        }
        if (exchangePoint < pointPara.getPoint2balanceMin()) {
            return ResultUtil.fail(re, "至少兑换" + pointPara.getPoint2balanceMin());
        }
        
        String timeunit = pointPara.getPointTimeUnit() == 1 ? "天": pointPara.getPointTimeUnit() == 2 ? "周" : "月";
        String totalunit = pointPara.getPointTotalUnit() == 1 ? "天": pointPara.getPointTotalUnit() == 2 ? "周" : "月";
        //  上限验证
        Long hasExchangedAmoutMonth = hasExchangeAmout(userId);
        if (hasExchangedAmoutMonth + exchangePoint > pointPara.getPoint2balanceTotalMax()) {
            re.putData("extra", true);//标识申请额外提现
            return ResultUtil.fail(re, "每"+totalunit+"最多兑换" + pointPara.getPoint2balanceTotalMax() + ",当前还可兑换" + (pointPara.getPoint2balanceTotalMax() - hasExchangedAmoutMonth));
        }
        
        if (exchangePoint > pointPara.getPoint2balancePerMax()) {
            return ResultUtil.fail(re, "每次最多兑换" + pointPara.getPoint2balancePerMax());
        }

        Long hasExchangedTimesMonth = hasExchangeTimes(userId);
        //  次数验证
        if (hasExchangedTimesMonth > pointPara.getPoint2balanceTimes()) {
            re.putData("extra", true);//标识申请额外提现
            return ResultUtil.fail(re, "每"+timeunit+"最多兑换" + pointPara.getPoint2balanceTimes() + "次");
        }

        return ResultUtil.success(re);
    }

    /**
     * 周期内已兑换积分
     * */
    public Long hasExchangeAmout(Integer userId){
        PointParaDO pointPara = sysParamsService.getPointPara();
        return fetchDao.sumTotal(setTime(pointPara.getPointTotalUnit()),userId);
    }

    /**
     * 周期内已使用次数
     * */
    public Long hasExchangeTimes(Integer userId){
        PointParaDO pointPara = sysParamsService.getPointPara();
        return fetchDao.countTimes(setTime(pointPara.getPointTimeUnit()),userId);
    }

    private Date setTime(Integer timeUnit) {
        Calendar c = Calendar.getInstance();
        Date d = null;
        if(timeUnit == 1){
            c.set(Calendar.HOUR_OF_DAY, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            d = c.getTime();
        }else if(timeUnit == 2){
            c.add(Calendar.WEEK_OF_YEAR, 0);
            c.set(Calendar.DAY_OF_MONTH, 1);
            d = c.getTime();
        }else {
            c.add(Calendar.MONTH, 0);
            c.set(Calendar.DAY_OF_MONTH, 1);
            d = c.getTime();
        }
        return d;
    }

    @Override
    public Result canExtraExchange(Integer userId, Integer extraPoint) {
        Result re = new Result();
        Customer customer = customerDao.selectByPrimaryKey(userId);
        if (customer.getVip() != 1) {
            return ResultUtil.fail(re, "抱歉，您不是我们的会员，不能享受积分兑换服务");
        }
        Integer point = customer.getPoint();
        if (point < extraPoint) {
            return ResultUtil.fail(re, "积分不足");
        }
        PointParaDO pointPara = sysParamsService.getPointPara();
        if(extraPoint%pointPara.getPoint2balanceScale() != 0){
            return ResultUtil.fail(re,"积分必须为"+pointPara.getPoint2balanceScale()+"的整数倍");
        }
        if (extraPoint < pointPara.getPoint2balanceExtraMin()) {
            return ResultUtil.fail(re, "额外兑换申请至少为" + pointPara.getPoint2balanceExtraMin() + "积分");
        }

        return ResultUtil.success(re);
    }

    @Override
    public void addVipTime(Integer userId, Item item) {
        Customer customer = selectById(userId);
        String[] expire = item.getExpiry().split(":");
        customer.setVip(Byte.parseByte("1"));
        Date vipEx = customer.getVipExpiretime();
        if (vipEx == null) {
            vipEx = new Date();
        }
        String unit = expire[1];
        Integer count = Integer.parseInt(expire[0]);
        if (unit.equalsIgnoreCase("d")) {
            vipEx = DateUtils.addDays(vipEx, count);
        } else if (unit.equalsIgnoreCase("w")) {
            vipEx = DateUtils.addWeeks(vipEx, count);
        } else if (unit.equalsIgnoreCase("m")) {
            vipEx = DateUtils.addMonths(vipEx, count);
        } else if (unit.equalsIgnoreCase("y")) {
            vipEx = DateUtils.addYears(vipEx, count);
        }
        customer.setVipExpiretime(vipEx);
        updateCustomer(customer);
    }

    @Override
    public List<CustomerMention> findAllBirthMention(Integer userId, Integer id) {
        return mentionDao.selectByUserId(userId, id);
    }

    @Override
    public Result insertOrUpdateMention(CustomerMention mention) {
        Result re = new Result();
        if (StringUtils.isEmpty(mention.getName()) || mention.getName().length() > 5) {
            return ResultUtil.fail(re, "姓名长度最多5位且不能为空");
        }
        if (StringUtils.isEmpty(mention.getRelation())) {
            return ResultUtil.fail(re, "关系不能为空");
        }
        if (mention.getBirthday() == null || mention.getBirthday().after(new Date())) {
            return ResultUtil.fail(re, "生日格式错误!");
        }
        mention.setType(1);
        if (null == mention.getId()) {
            mentionDao.insert(mention);
        } else {
            mentionDao.update(mention);
        }

        return ResultUtil.success(re);
    }

    @Override
    public void deleteMention(Integer id, Integer userId) {

        mentionDao.deleteByIdAndUserId(id, userId);
    }

    @Override
    public void beNextLevel(Integer fUserId, Integer sonId) {
        Customer customer = customerDao.selectByPrimaryKey(sonId);
        customer.setPid(fUserId);
        customerDao.updateByPrimaryKeySelective(customer);
    }

    @Override
    public PageInfo<SpreadVO> findSpread(Integer userId, Integer pageSize, Integer currenPage) {
        PageHelper.startPage(currenPage, pageSize);
        List<SpreadVO> list = spreadDao.findSpreadvo(userId);
        return new PageInfo<>(list);
    }


    /**
     * 删除收货地址
     */
    @Transactional
    public void deleteAddress(Integer id) {
        addressDao.deleteByPrimaryKey(id);
    }

    /**
     * 绑定手机发送验证码
     */
    public Result sendCode(String mobile, Integer userId) {
        Result re = new Result();
        if (!MobilePhoneUtil.isVaildAdress(mobile)) {
            return ResultUtil.fail(re, "手机号格式错误！");
        }
        if (isExsitMobile(mobile, userId)) {
            return ResultUtil.fail(re, "手机号已绑定！");
        }
        //TODO  发送验证码
        re = smsService.SendCode(SmsCodeType.bingMobile,mobile);
        return re;
    }

    /**
     * 绑定手机号
     */
    @Transactional
    public Result bingMobile(String mobile, Integer userId, String code) {
        Result re = new Result();
        if (!smsService.validateCode(SmsCodeType.bingMobile,mobile, code)) {
            return ResultUtil.fail(re, "验证码错误");
        }
        if(customerDao.hasBinding(mobile) != null){
            return ResultUtil.fail(re, "手机号已绑定！");
        }

        Customer customer = customerDao.selectByPrimaryKey(userId);
        customer.setTel(mobile);
        customerDao.updateByPrimaryKeySelective(customer);
        return ResultUtil.success(re);
    }

    /**
     * 手机号是否存在
     */
    private boolean isExsitMobile(String mobile, Integer userId) {
        return customerDao.isExsitMobile(mobile, userId) != null;
    }

    /**
     * 获得当前客户和上级客户
     *
     * @param userId
     * @return
     */
    @Override
    public Result findCustomerAndHigherCustomer(Integer userId) {
        Result result = new Result();
        Customer currentCustomer = selectById(userId);
        result.putData("currentCustomer", currentCustomer);
        return result;
    }

    /**
     * 分销送积分/余额
     */
    @Transactional
    @Override
    public void spread(OrderMain order) {
        // 根据商品查询返利总额
        Customer customer = customerDao.selectByPrimaryKey(order.getCustomerId());
        Integer pid = customer.getPid();
        if (null == pid || pid == 0) {
            return;
        }

        BigDecimal total = itemDao.sumTotalByOrderId(order.getId());
        if (total.compareTo(BigDecimal.ZERO) > 0) {
            AppParamEntity entity = new AppParamEntity();
            entity.setCode("DISTRIBUTION");
            List<DistributIonDO> pdo = paramService.getDistributionDOs(entity);
            BigDecimal hasSpread = BigDecimal.ZERO;
            SpreadSale spreadSale = new SpreadSale();
            spreadSale.setOrderId(order.getId());
            spreadSale.setTotalAmount(total);
            spreadSale.setCustomerId(order.getCustomerId());
            spreadSaleDao.insert(spreadSale);

            for (DistributIonDO ddo : pdo) {
                String sql = "";
                String temp = "";
                for (int i = 0; i < ddo.getIndex(); i++) {
                    sql += "select n_pid from ms_customer where n_id =(";
                    temp += ")";
                }
                sql = sql + customer.getId() + temp;
                Integer receiverId = customerDao.findByLevelId(sql);
                Result<Customer> result = isExist(receiverId);
                Customer recevier = null;
                if (ResultUtil.isSuccess(result)) {
                    recevier = result.getReturnValue();
                }
                BigDecimal amount = new BigDecimal(ddo.getRate()/100 * total.doubleValue() + "");
                if (null != recevier && hasSpread.add(amount).compareTo(total) < 0) {
                    SpreadSaleAllot saleAllot = new SpreadSaleAllot();
                    saleAllot.setAllotTime(new Date());
                    saleAllot.setAllotType(ddo.getType());
                    saleAllot.setSpreadSaleId(spreadSale.getId());
                    saleAllot.setReceiverCustomerId(recevier.getId());
                    //1送余额 2送积分
                    if (ddo.getType() == 1) {
                        saleAllot.setAmount(amount);
                        BalanceRecordDO bdo = new BalanceRecordDO();
                        bdo.setAmount(amount);
                        bdo.setType(BalanceType.spread_sale);
                        bdo.setCustomerId(Long.parseLong(recevier.getId() + ""));
                        bdo.setDesc("分销获得");
                        Integer id = balanceService.getBalance(bdo);
                        saleAllot.setPointOrBalanceRecordId(id);
                    } else {
                        BigDecimal pointAmout = new BigDecimal(amount.divide(sysParamsService.getPointPara().getPointScale()) + "");
                        pointAmout = new BigDecimal(pointAmout.intValue() + "");
                        if (pointAmout.intValue() > 0) {
                            saleAllot.setAmount(new BigDecimal(pointAmout.intValue()+""));
                            PointOrderDO pointOrderDO = new PointOrderDO();
                            pointOrderDO.setPoint(pointAmout.intValue());
                            pointOrderDO.setType(PointBizType.spread_sale_get);
                            pointOrderDO.setCustomerId(Long.parseLong(recevier.getId() + ""));
                            Long id = pointService.spreadGet(pointOrderDO).getReturnValue();
                            saleAllot.setPointOrBalanceRecordId(id.intValue());
                        }
                    }
                    spreadDao.insert(saleAllot);
                    hasSpread = hasSpread.add(amount);
                }
            }
            spreadSale.setAllotAmount(hasSpread);
            spreadSaleDao.update(spreadSale);
        }
    }

    @Override
    public BigDecimal sumSpreadBalance(Integer userId) {
        return spreadSaleDao.sumBalance(userId);
    }

    @Override
    public BigDecimal sumSpreadPoint(Integer userId) {
        return spreadSaleDao.sumPoint(userId);
    }

    @Override
    public PageInfo<Customer> findRecomend(Integer userId, int currentPage, int pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Customer> customers = customerDao.findByPid(userId);
        return new PageInfo<>(customers);
    }


}
